app.controller('MailCtrl', ['$scope', function ($scope) {
    $scope.folds = [
        {name: 'Inbox', filter: ''},
        {name: 'Starred', filter: 'starred'},
        {name: 'Sent', filter: 'sent'},
        {name: 'Important', filter: 'important'},
        {name: 'Draft', filter: 'draft'},
        {name: 'Trash', filter: 'trash'}
    ];

    $scope.labels = [
        {name: 'Angular', filter: 'angular', color: '#23b7e5'},
        {name: 'Bootstrap', filter: 'bootstrap', color: '#7266ba'},
        {name: 'Client', filter: 'client', color: '#fad733'},
        {name: 'Work', filter: 'work', color: '#27c24c'}
    ];

    $scope.addLabel = function () {
        $scope.labels.push(
            {
                name: $scope.newLabel.name,
                filter: angular.lowercase($scope.newLabel.name),
                color: '#ccc'
            }
        );
        $scope.newLabel.name = '';
    }

    $scope.labelClass = function (label) {
        return {
            'b-l-info': angular.lowercase(label) === 'angular',
            'b-l-primary': angular.lowercase(label) === 'bootstrap',
            'b-l-warning': angular.lowercase(label) === 'client',
            'b-l-success': angular.lowercase(label) === 'work'
        };
    };

}]);

app.controller('MailListCtrl',
               ['$scope', 'mails', '$stateParams', function ($scope, mails, $stateParams) {
                   $scope.fold = $stateParams.fold;
                   mails.all().then(function (mails) {
                       $scope.mails = mails;
                   });
               }]);

app.controller('MailDetailCtrl',
               ['$scope', 'mails', '$stateParams', function ($scope, mails, $stateParams) {
                   mails.get($stateParams.mailId).then(function (mail) {
                       $scope.mail = mail;
                   })
               }]);

app.controller('MailNewCtrl', ['$scope', 'toaster', '$http', function ($scope, toaster, $http) {
    $scope.mail = {
        emailTos: [],
        subject: '',
        content: ''
    }
    $scope.toaster = {};
    $scope.tolist = [
        {name: 'James', email: 'zmloper01@163.com'},
        {name: 'Luoris Kiso', email: '970223838@qq.com'},
        {name: 'Lucy Yokes', email: '1111@11.com'}
    ];
    var testEditor;
    $scope.init = function () {
        testEditor = editormd("test-editormd", {
            width: "100%",
            height: 450,
            path: '/static/editormd/lib/',
            // theme: "dark",
            // previewTheme: "dark",
            // editorTheme: "pastel-on-dark",
            codeFold: true,
            //syncScrolling : false,
            saveHTMLToTextarea: true,    // 保存 HTML 到 Textarea
            searchReplace: true,
            //watch : false,                // 关闭实时预览
            htmlDecode: "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启
            //toolbar  : false,             //关闭工具栏
            //previewCodeHighlight : false, // 关闭预览 HTML 的代码块高亮，默认开启
            emoji: true,
            taskList: true,
            tocm: true,         // Using [TOCM]
            tex: true,                   // 开启科学公式TeX语言支持，默认关闭
            flowChart: true,             // 开启流程图支持，默认关闭
            sequenceDiagram: true,       // 开启时序/序列图支持，默认关闭,
            //dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
            //dialogShowMask : false,     // 设置弹出层对话框显示透明遮罩层，全局通用，默认为true
            //dialogDraggable : false,    // 设置弹出层对话框不可拖动，全局通用，默认为true
            //dialogMaskOpacity : 0.4,    // 设置透明遮罩层的透明度，全局通用，默认值为0.1
            //dialogMaskBgColor : "#000", // 设置透明遮罩层的背景颜色，全局通用，默认为#fff
            imageUpload: true,
            imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
            imageUploadURL: "/blog/uploadfile",
            init: function () {
                console.log('onload', this);

                //this.fullscreen();
                //this.unwatch();
                //this.watch().fullscreen();
                //this.setMarkdown("#PHP");
                //this.width("100%");
                //this.height(480);
                //this.resize("100%", 640);
            }
        });
    };
    $scope.init();
    $scope.pop = function () {
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    $scope.send = function () {
        $scope.mail.content = testEditor.getHTML();
        $http.post('email/send', $scope.mail).success(function (data) {
            if (data.status == 0) {
                $scope.toaster = {
                    type: 'success',
                    title: 'Mail',
                    text: 'send mail Success!!!'
                }
                $scope.pop();
            }else {
                $scope.toaster = {
                    type : 'error',
                    title : 'Mail',
                    text : data.errMsg
                }
            }
        })
    }
}]);

angular.module('app').directive('labelColor', function () {
    return function (scope, $el, attrs) {
        $el.css({'color': attrs.color});
    }
});